/*
 * 验证表单
 * @param {Object} formRegExp 
 * {objId:'regEmail',regExp:regEmail,required:true,msgObjId:'msgRegEmail',requiredMsg:'请输入电子邮件！',msg:'请输入正确的电子邮件！'}
 * {cls:'input_txt',errCls:'input_txt_err',errLblCls:'err'}
 */
function validateForm(formRegExps,formCls){
	var hasFailed = false;
	for(i in formRegExps){
		var formRegExp = formRegExps[i];//获取验证规则
		var val =$.trim($("#"+formRegExp.objId).val());
		$("#"+formRegExp.objId).val(val);//去除两边空格重新赋值
		
		//判断是否是必填选项
		if(formRegExp.required){
			//判断该选项是否有输入值
			//如果为空则给出提示
			if(""==val || null == val){
				if(!hasFailed){
					$("#"+formRegExp.objId).focus();//设置焦点
					hasFailed = true;
				}
				$("#"+formRegExp.msgObjId).html(formRegExp.requiredMsg);
				setCalss(formRegExp,formCls,true);
				continue;
			}
		}else{
			if("" ==val || null == val){
				setCalss(formRegExp,formCls,false);
				continue;
			}
		}
		
		//判断是否有验证规则
		if(null!=formRegExp.regExp){
			if(!formRegExp.regExp.test(val)){
				if(!hasFailed){
				 	$("#"+formRegExp.objId).focus();
				 	hasFailed = true;
				}
				$("#"+formRegExp.msgObjId).html(formRegExp.msg);
				setCalss(formRegExp,formCls,true);
				continue;
			}
		}
		
		//判断是否含有相同验证
		if(null!=formRegExp.sameObjId){
			if($.trim($("#"+formRegExp.sameObjId).val()) != val){
				if(!hasFailed){
				 	$("#"+formRegExp.objId).focus();
				 	hasFailed = true;
				}
				$("#"+formRegExp.msgObjId).html(formRegExp.sameMsg);
				setCalss(formRegExp,formCls,true);
				continue;
			}
		}
		setCalss(formRegExp,formCls,false);
	}
	
	
	
	return !hasFailed;//返回值
}

function setCalss(formRegExp,formCls,isFaided){
	if(isFaided){
		//$("#"+formRegExp.msgObjId).removeClass();
		$("#"+formRegExp.msgObjId).addClass(formCls.errLblCls);
		//$('#'+formRegExp.objId).removeClass();
		$('#'+formRegExp.objId).addClass(formCls.errCls);
	}else{
		$("#"+formRegExp.msgObjId).html("");
		$("#"+formRegExp.msgObjId).removeClass(formCls.errLblCls);
		$('#'+formRegExp.objId).removeClass(formCls.errCls);
		//$('#'+formRegExp.objId).addClass(formCls.cls);
	}
}
